﻿# coding: utf-8

import Transformation
import Math
import SceneNode

from Command import CCommand

class CCmdRotation(CCommand):
    def __init__(self, **p):
        """
        Default class contructor.
        
        Parameters:
        @param nodeRef - the weak reference of the scene node
        @param newRotation - the rotation quaternion value (x, y, z, w)
        """
        
        CCommand.__init__(self, TD.CMD_ROTATION, **p)        
        
    def DoCmd(self):
        try:
            nodeRef = self._paramDict['nodeRef']
            x, y, z, w = self._paramDict['newRotation']
            
            node = nodeRef()
            coreHandle = node.GetCoreHandle()
            
            # save the old rotation
            self._x, self._y, self._z, self._w = Transformation.GetRelativeRotation(SceneNode._CTransformation(coreHandle))
            
            # set the new rotation
            Transformation.SetRelativeRotation(SceneNode._CTransformation(coreHandle), x, y, z, w)
            
        except:
            SHOW_TRACE()
            
    def UndoCmd(self):
        try:
            nodeRef = self._paramDict['nodeRef']
            node = nodeRef()
            coreHandle = node.GetCoreHandle()
            
            # restore the old rotation
            Transformation.SetRelativeRotation(SceneNode._CTransformation(coreHandle), self._x, self._y, self._z, self._w)
            
        except:
            SHOW_TRACE()
            
    def UpdateRotation(self, dy, dp, dr):
        try:
            nodeRef = self._paramDict['nodeRef']
            node = nodeRef()
            coreHandle = node.GetCoreHandle()

            # restore the old rotation
            x, y, z, w = Math.QuaternionFromYawPitchRoll(dy, dp, dr)
            x, y, z, w = Math.QuaternionMultiply(self._x, self._y, self._z, self._w, x, y, z, w)
            self._paramDict['newRotation'] = (x, y, z, w)
            Transformation.SetRelativeRotation(SceneNode._CTransformation(coreHandle), x, y, z, w)

        except:
            SHOW_TRACE()            

        return True